package com.ruoyi.zhgd.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.ZjWarningDisposalVo;
import com.ruoyi.zhgd.domain.bo.ZjWarningDisposalBo;
import com.ruoyi.zhgd.service.IZjWarningDisposalService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警处置
 *
 * @author zyb
 * @date 2024-03-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/warningDisposal")
public class ZjWarningDisposalController extends BaseController {

    private final IZjWarningDisposalService iZjWarningDisposalService;

    private final ISysDeptService deptService;

    /**
     * 查询预警处置列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<ZjWarningDisposalVo> list(ZjWarningDisposalBo bo, PageQuery pageQuery) {

        return iZjWarningDisposalService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出预警处置列表
     */
    @SaCheckLogin
    @Log(title = "预警处置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZjWarningDisposalBo bo, HttpServletResponse response) {
        List<ZjWarningDisposalVo> list = iZjWarningDisposalService.queryList(bo);
        ExcelUtil.exportExcel(list, "预警处置", ZjWarningDisposalVo.class, response);
    }

    /**
     * 获取预警处置详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<ZjWarningDisposalVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iZjWarningDisposalService.queryById(id));
    }

    /**
     * 获取预警处置详细信息根据预警id和预警类别；
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/getInfoByWarning")
    public R<ZjWarningDisposalVo> getInfoByWarning(@NotNull(message = "主键不能为空")
                                          @RequestParam("id") String id,@RequestParam("type") int type) {
        return R.ok(iZjWarningDisposalService.getInfoByWarning(id,type));
    }

    /**
     * 新增预警处置
     */
    @SaCheckLogin
    @Log(title = "预警处置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZjWarningDisposalBo bo) {
        return toAjax(iZjWarningDisposalService.insertByBo(bo));
    }

    /**
     * 修改预警处置
     */
    @SaCheckLogin
    @Log(title = "预警处置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZjWarningDisposalBo bo) {
        return toAjax(iZjWarningDisposalService.updateByBo(bo));
    }

    /**
     * 驳回、同意预警处置
     */
    @SaCheckLogin
    @RepeatSubmit()
    @PostMapping("/handleApproval")
    public R<Void> handleApproval(@RequestBody ZjWarningDisposalBo bo) {
        return toAjax(iZjWarningDisposalService.handleApproval(bo));
    }

    /**
     * 删除预警处置
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "预警处置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iZjWarningDisposalService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询发送人用户列表
     */
    @SaCheckLogin
    @GetMapping("/userList")
    public R<List<SysUser>> userList() {

        SysDept sysDept = deptService.selectDeptById(LoginHelper.getDeptId());
        if (sysDept == null) {
            return R.ok();
        }
        String code = sysDept.getCode();
        String substring = null;
        String ancestor = null;
        if (code == null) {
            return R.ok();
        }
        /**
         * 济微公司
         */
        if (code.startsWith("JD")) {
            if (sysDept.getDeptName().length() >= 3) {
                substring = sysDept.getDeptName().substring(0, 3) + "驻地办";
            }
            /**
             * 文登公司
             */
        } else if (code.startsWith("WD")) {
            ancestor = sysDept.getAncestors();
            substring = "总监办";
            /**
             * 齐东公司
             */
        } else if (code.startsWith("QD")) {
            ancestor = sysDept.getAncestors();
            substring = "总监办";
        } else {
            return R.ok();
        }
        return iZjWarningDisposalService.selectUserList(substring, ancestor);
    }

}
